package com.fuluwa.domain.shared;

import java.io.Serializable;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.MappedSuperclass;

import com.fuluwa.infra.exception.PersistenceException;

@MappedSuperclass
@Access(AccessType.FIELD)
public abstract class AbstractDomainObject implements Serializable{

	public void save() throws PersistenceException {
		DataAccess.getInstance().save(this);
	}

	protected abstract Serializable getDomainId();

	@SuppressWarnings("unchecked")
	public <T> T load() throws PersistenceException {
		return (T) DataAccess.getInstance().find(this.getClass(), getDomainId());
	}

	public int count() throws PersistenceException {
		return DataAccess.getInstance().count(this.getClass());
	}

	public void delete() throws PersistenceException {
		DataAccess.getInstance().delete(this);
	}

	public void update() throws PersistenceException {
		DataAccess.getInstance().update(this);
	}
	public void detach() throws PersistenceException {
		DataAccess.getInstance().detach(this);
	}

	/*-public boolean exists() {
		return crudRepository.exists(domainId);
	}

	public Iterable<T> findAll() {
		return crudRepository.findAll();
	}

	public Iterable<T> findAll(Iterable<ID> ids) {
		return crudRepository.findAll(ids);
	}
	}
	 */
	public void validate() {

	}
}
